package com.zcsy.manage.service.mall.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.mall.IntegralConfig;
import com.zcsy.manage.model.mall.IntegralRecord;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.mall.IntegralConfigDao;
import com.zcsy.manage.persistence.mall.IntegralRecordDao;
import com.zcsy.manage.persistence.system.UserDao;
import com.zcsy.manage.service.mall.IIntegralRecordService;


/**
 * @Title: IntegralRecordServiceImpl.java
 * @Package： com.zcsy.manage.persistence.mall 
 * @Description: 积分变更实体类
 * @author yangfan 373188993@qq.com
 * @Copyright: 2016 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年7月1日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class IntegralRecordServiceImpl implements IIntegralRecordService {
    
    @Autowired
    private IntegralRecordDao integralRecordDao;
    
    @Autowired
    private IntegralConfigDao integralConfigDao;
    
    @Autowired
    private UserDao userDao;
    
    @Override
    public int insert(IntegralRecord integralRecord) {
        return integralRecordDao.insert(integralRecord);
    }
    
    @Override
    public List<IntegralRecord> selectByUserAccount(IntegralRecord integralRecord) {
        return integralRecordDao.selectByUserAccount(integralRecord);
    }
    
    public IntegralRecord selectLastRecordByUserAccountAndDespription(IntegralRecord integralRecord) {
        return integralRecordDao.selectLastRecordByUserAccountAndDespription(integralRecord);
    }
    
    /**
     * @Description 积分增减公共方法 
     * @param userAccount  用户账户名
     *        integralName 积分配置名称
     * @return result成功或失败
     * @author yangfan 373188993@qq.com
     * @date 2016年7月10日
     */
    public int changeIntegralMethod(String userAccount, String integralName) {
        
        IntegralRecord integralRecord = new IntegralRecord();

        // 生成18位id
        String id = UniqueNumberUtils.get18UniqueNumber();

        // 查询积分配置的相关信息
        IntegralConfig integralConfig = integralConfigDao.selectByName(integralName);
        
        //查询用户积分信息
        User record = userDao.selectByLoginName(userAccount);

        // 积分变动信息
        integralRecord.setId(id);
        integralRecord.setUserAccount(userAccount);
        integralRecord.setRecordTime(DateUtils.gettimestamp());
        integralRecord.setDescription(integralConfig.getLabel());
        integralRecord.setChange(integralConfig.getValue());

        
        // 保存积分变更信息,返回1：成功 ，0：失败
        int flag = integralRecordDao.insert(integralRecord);
        
        if (flag == OwnerConstants.INSERT_SUCCESS) {
            // 更新积分
            int sum = (integralRecord.getChange() + record.getIntegral());
            record.setIntegral(sum);
            userDao.updateByPrimaryKeySelective(record);
        }

        return flag;
    }
    
    @Override
    public List<IntegralRecord> selectRecordByUserAccount(String userAccount) {
        return integralRecordDao.selectRecordByUserAccount(userAccount);
    }
    
    public IntegralRecord selectTodaySignInRecordByUserAccount(String userAccount) {
        return integralRecordDao.selectTodaySignInRecordByUserAccount(userAccount);
    }

    @Override
    public IntegralRecord selectTodayRecord(IntegralRecord integralRecord) {
        return integralRecordDao.selectTodayRecord(integralRecord);
    }
    
    @Override
    public Integer getIntegralOfToday(String userId) {
        
        // 获取当前用户当天的积分变更记录
        List<IntegralRecord> integralRecordList = integralRecordDao.selectRecordByUserAccount(userId);
        // 当前用户当天积分变动之和
        Integer soFarIntegralSum = 0;
        // 求当前用户当天积分变动之和
        for (int i = 0; i < integralRecordList.size(); i++) {
            soFarIntegralSum += integralRecordList.get(i).getChange();
        }
        
        return soFarIntegralSum;
    }
    
    @Override
    public boolean testWhetherUserGetTheIntegral(String userId,String descrption) {
        
        // new一个积分变动记录
        IntegralRecord integralRecord = new IntegralRecord();
        // 根据用户的账号和描述在积分表中获取该用户的积分变动信息（为null则是没有获得过该项目的积分）
        integralRecord.setUserAccount(userId);
        integralRecord.setDescription(descrption);
        // 查询用户当天签到拿积分的积分变更记录
        IntegralRecord todayRecord = integralRecordDao.selectTodayRecord(integralRecord);
        if(null != todayRecord) {
            return false;
        } else {
            return true;
        }
        
    }
    
    @Override
    public int changeIntegralWhenToMaxValuePerDay(String userAccount, String integralName, Integer value) {
        IntegralRecord integralRecord = new IntegralRecord();

        // 生成18位id
        String id = UniqueNumberUtils.get18UniqueNumber();

        // 查询积分配置的相关信息
        IntegralConfig integralConfig = integralConfigDao.selectByName(integralName);
        
        //查询用户积分信息
        User record = userDao.selectByLoginName(userAccount);

        // 积分变动信息
        integralRecord.setId(id);
        integralRecord.setUserAccount(userAccount);
        integralRecord.setRecordTime(DateUtils.gettimestamp());
        integralRecord.setDescription(integralConfig.getLabel());
        integralRecord.setChange(value);

        // 保存积分变更信息,返回1：成功 ，0：失败
        int flag = integralRecordDao.insert(integralRecord);
        
        if (flag == OwnerConstants.INSERT_SUCCESS) {
            // 更新积分
            int sum = (integralRecord.getChange() + record.getIntegral());
            record.setIntegral(sum);
            userDao.updateByPrimaryKeySelective(record);
        }

        return flag;
    }
}
